﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace BasicCMS.Data
{
    /// <summary>
    /// 工作单元，用于时行数据库处理
    /// </summary>
    public class UnitOfWork : IUnitOfWork
    {
        private IList<IPersistentBuilder> persistentBuilders = new List<IPersistentBuilder>();

        private UnitOfWork() { }

        /// <summary>
        /// 开启一个新的工作单元
        /// </summary>
        /// <returns></returns>
        public static IUnitOfWork Begin()
        {
            return new UnitOfWork();
        }

        public InsertBuilder Insert(string tableName)
        {
            var builder = new InsertBuilder(this, tableName);
            persistentBuilders.Add(builder);
            return builder;
        }

        public UpdateBuilder Update(string tableName)
        {
            var builder = new UpdateBuilder(this, tableName);
            persistentBuilders.Add(builder);
            return builder;
        }

        public DeleteBuilder Delete(string tableName)
        {
            var builder = new DeleteBuilder(this, tableName);
            persistentBuilders.Add(builder);
            return builder;
        }

        public ExecuteBuilder Execute(string sql)
        {
            var builder = new ExecuteBuilder(this, sql);
            persistentBuilders.Add(builder);
            return builder;
        }

        QueryBuilder IUnitOfWork.Query(string sql)
        {
            var builder = new QueryBuilder(this, sql);
            builder.Command = new SqlCommand { Connection = Connection };
            return builder;
        }



        public QueryTableBuilder Table(string tableName)
        {
            var builder = new QueryTableBuilder(this, tableName);
            builder.Command = new SqlCommand { Connection = Connection };
            return builder;
        }

        public void Commit()
        {
            foreach (var item in persistentBuilders)
            {
                if (item.IsValid)
                {
                    SqlCommand command = new SqlCommand { Connection = Connection };
                    item.Command = command;
                    item.Persistent();
                }
            }
        }

        private SqlConnection connection;

        private SqlConnection Connection
        {
            get
            {
                if (connection == null)
                {
                    connection = new SqlConnection("Data Source=.;Initial Catalog=BasicCMS;User Id=sa;Password=123;Max Pool Size=1000;Min Pool Size=5;Pooling=True;");
                    connection.Open();
                }
                return connection;
            }
        }

        public void Dispose()
        {
            if (connection != null && connection.State!=ConnectionState.Closed)
            {
                try
                {
                    connection.Dispose();
                    connection = null;
                }
                catch { }
            }
        }

    }
}
